package com.qianclass.talklive.app.enter;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianclass.talklive.app.utils.Constant;
import com.qianclass.talklive.app.utils.Info;
import com.qianclass.talklive.app.utils.LogUtil;
import com.qianclass.talklive.app.utils.StringEncrypt;
import com.qianclass.talklive.app.utils.VerificationKeyUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by admin on 2017/3/8.
 * <p>
 * 更新时间 2017/3/8
 * 更新描述 ${TODO}
 */

public class EnterModelImpl implements EnterModel {
    String url;
    String sub;

    @Override
    public void userEnterCourse(String RoomID, String Token, final String AgencyID, final OnEnterListener onEnterListener) {
        onEnterListener.showProgress();

        /**
         Version：字符串，接口的版本号，与本文档版本一致。
         UA：字符串，客户端类型。iOS/Android/Windows/MacOS。
         RoomID：教室 ID。字符串。
         Token：字符串，从 agent_user_login 接口获得。
         VerificationKey：密文。机构+时间字符串
         */
//        DateTime dateTime = new DateTime();
//        String time = dateTime.getDateTime();
//        String VerificationKey = StringEncrypt.S1Encrypt(Info.MECHANISM_ID + "+" + time, Info.API_EncryptionKey);
        VerificationKeyUtils keyUtils = new VerificationKeyUtils();
        String VerificationKey = keyUtils.getVerificationKey(AgencyID);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.addBodyParameter("UA", "Android");
        params.addBodyParameter("Version", Info.APIVERSION);
        params.addBodyParameter("RoomID", RoomID);
        params.addBodyParameter("Token", Token);
        params.addBodyParameter("VerificationKey", VerificationKey);

        httpUtils.send(HttpRequest.HttpMethod.POST, Constant.getEnterRoomUrl(), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                LogUtil.i("MVP","EnterModelImpl "+responseInfo.result);
                try {
                    JSONObject responseJson = new JSONObject(responseInfo.result);
                    if(responseJson.has("Result")){
                        String result = responseJson.getString("Result");
                        if("true".equals(result)){//
                            if (responseInfo.result.contains("URL")) {
                                url = responseJson.getString("URL");
                                //sub = url.substring(9);
                                sub = url.split("://")[1];
                            } else if (responseInfo.result.contains("url")) {
                                url = responseJson.getString("url");
                                //sub = url.substring(9);
                                sub = url.split("://")[1];
                            }
                            if (sub != null) {
                                final String decryptJson = StringEncrypt.S1Decrypt(sub, Info.Protocol_EncryptionKey);
                                JSONObject jsonObject = new JSONObject(decryptJson);
                                String command = jsonObject.getString("Command");
                                if(command.equals("EnterRoom")){
                                    LogUtil.i("MVP","decryptJson "+decryptJson);
                                    onEnterListener.hideProgress();
                                    onEnterListener.enterSuc(decryptJson);

                                }else{//短协议
                                    String urlid = jsonObject.getString("URLID");

                                    VerificationKeyUtils keyUtils = new VerificationKeyUtils();
                                    String VerificationKey = keyUtils.getVerificationKey(AgencyID);
                                    HttpUtils httpUtils = new HttpUtils();
                                    RequestParams params = new RequestParams();
                                    params.addBodyParameter("Version", Info.APIVERSION);
                                    params.addBodyParameter("URLID", urlid);
                                    params.addBodyParameter("VerificationKey", VerificationKey);
                                    httpUtils.send(HttpRequest.HttpMethod.POST, Constant.getEnterRoomCommandUrl(), params, new RequestCallBack<String>() {
                                        @Override
                                        public void onSuccess(ResponseInfo<String> responseInfo) {
                                            try {
                                                JSONObject json = new JSONObject(responseInfo.result);
                                                String result = json.getString("Result");
                                                if(result.equals("true")){
                                                    String command = json.getString("Command");
                                                    //sub = command.substring(9);
                                                    sub = command.split("://")[1];
                                                    if (sub != null) {
                                                        String decrypt = StringEncrypt.S1Decrypt(sub, Info.Protocol_EncryptionKey);
                                                        onEnterListener.enterSuc(decrypt);
                                                        onEnterListener.hideProgress();
                                                    }


                                                }
                                            } catch (JSONException e) {
                                                onEnterListener.hideProgress();
                                                e.printStackTrace();
                                            }
                                        }

                                        @Override
                                        public void onFailure(HttpException e, String s) {
                                            onEnterListener.hideProgress();
                                        }
                                    });


                                }

                            }

                        }else if ("false".equals(result)){
                            onEnterListener.hideProgress();
                            onEnterListener.enterFal(1, responseJson.getString("code"));
                        }
                    }
                } catch (JSONException e) {
                    onEnterListener.hideProgress();
                    onEnterListener.enterFal(1, e.getMessage());
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                onEnterListener.enterFal(0, s);
                onEnterListener.hideProgress();
            }
        });
    }


}
